package healthylife.top.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.stream.Stream;

@Slf4j
public class ProcessBar {
    private static final int TOTAL = 100;
    private static final char INCOMPLETE = 9617; // '░'
    private static final char COMPLETE = 9608;  // '█'
    private static final StringBuilder builder;

    static {
        builder = new StringBuilder();
        Stream.generate(() -> INCOMPLETE).limit(TOTAL).forEach(builder::append);
    }

    public static void main(String[] args) {
        progressBar();
    }

    public static void progressBar() {
        for (int i = 0; i < TOTAL; i++) {
            updateProgress(i + 1);
            if (i == TOTAL - 1) {
                System.out.println("");
                System.out.println("加载完成!");
            }
            try {
                Thread.sleep(i * 2L);
            } catch (InterruptedException ignored) {
            }
        }
    }

    public static void updateProgress(int progress) {
        for (int i = 0; i < progress; i++) {
            builder.replace(i, i + 1, String.valueOf(COMPLETE));
        }
        String progressBar = "\r" + builder;
        String percent = " " + progress + "%";
        System.out.print(progressBar + percent);
    }
}